package com.nextbusmn.server.JSONBuilders;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.google.appengine.labs.repackaged.org.json.JSONArray;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.appengine.labs.repackaged.org.json.JSONObject;
import com.nextbusmn.server.JDOWrappers.BusStopJDOWrapper;
import com.nextbusmn.server.dataobjects.BusStopData;
import com.nextbusmn.server.dataobjects.StopTimeData;

public class RouteJSONBuilder {
	
	public static JSONArray getRouteJSON(List<StopTimeData> stopTimes) {
		
		Set<Integer> stops = new HashSet<Integer>();
		for (StopTimeData std : stopTimes) {
			stops.add(std.getStopID());
		}
		
		List<BusStopData> stopData = BusStopJDOWrapper.getStopsByStopIDs(stops);
		
		JSONArray returnArray = new JSONArray();
		try {
			
			for (BusStopData row : stopData) {
				JSONObject obj = new JSONObject();
				
				obj.put("i", row.getStopID());  
				obj.put("n", row.getStopName());
				obj.put("d", row.getStopDesc());
				obj.put("x", (int) Math.floor(row.getStopLon() * 1000000.0));
				obj.put("y", (int) Math.floor(row.getStopLat() * 1000000.0));
				returnArray.put(obj);
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return returnArray;
	}
}
